package com.usian.wemedia.service.impl;/*
 *文件名：WmNewsServiceImpl
 *创建者：宋璟
 *创建时间:2023/2/13 16:52
 *描述: TODO
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.constans.wemedia.WemediaContans;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNews;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMapper;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class WmNewsServiceImpl extends BaseServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {

    @Value("${fdfs.url}")
    private String fdfs;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public ResponseResult findAll(WmNewsPageReqDto wmNewsPageReqDto) {
        if (wmNewsPageReqDto == null) return errorParam;
        //非必传参数
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        if (wmNewsPageReqDto.getStatus() != null)wrapper.eq(WmNews::getStatus,wmNewsPageReqDto.getStatus());
        if (wmNewsPageReqDto.getChannelId() != null)wrapper.eq(WmNews::getChannelId,wmNewsPageReqDto.getChannelId());
        if (wmNewsPageReqDto.getKeyword() != null)wrapper.eq(WmNews::getTitle,wmNewsPageReqDto.getKeyword());
        //必传参数
        if (wmNewsPageReqDto.getMaxTime() == null)wmNewsPageReqDto.setMaxTime(new Date());
        wrapper.lt(WmNews::getPublishTime,wmNewsPageReqDto.getMaxTime());
        wrapper.orderByDesc(WmNews::getPublishTime);
        if (ThreadLocalUtils.getUserId() == null) return needLogin;
        wrapper.lt(WmNews::getPublishTime,wmNewsPageReqDto.getMaxTime());
        return getPage(wmNewsPageReqDto,wrapper);
    }

    @Override
    public ResponseResult saveNews(WmNewsDto wmNews, Short isSubmit) {
        if (wmNews == null) return errorParam;
        WmNews news = new WmNews();
        BeanUtils.copyProperties(wmNews,news);
        if (wmNews.getId() == null){
            save(news);
            Integer newsId = news.getId();
            String content = news.getContent();
            JSONArray objects = JSON.parseArray(content);
            ArrayList<String> strings = new ArrayList();
            for (Object object : objects) {
                JSONObject jsonObject = JSON.parseObject(object.toString());
                if (jsonObject.get("type").equals("image")){
                    String value = jsonObject.getString("value");
                    strings.add(value.replace(fdfs,""));
                }
            }
            LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WmMaterial::getUserId,ThreadLocalUtils.getUserId());
            wrapper.in(WmMaterial::getUrl,strings);
            List<WmMaterial> list = wmMaterialMapper.selectList(wrapper);
            List<String> collect = list.stream().map(m -> m.getId() + "").collect(Collectors.toList());
            wmNewsMaterialMapper.saveRelationsByContent(collect,newsId, WemediaContans.WM_CONTENT_REFERENCE);
        }else {
            updateById(news);
        }
        return null;
    }


}
